<?php
namespace app\common\model;
use think\Model;
class Base extends Model
{
    protected function initialize()
    {
        parent::initialize();
    }

    /* 单条 */
    public static function getOne ($where,$field="*",$ord=false) {
        if($ord) {
            $res = self::where($where)->field($field)->order($ord)->find();
        }else{
            $res = self::where($where)->field($field)->find();
        }
        if(!$res) {
            return false;
        }
        return $res->toArray();
    }

    /* 单条 */
    public static function getOneById ($id,$field="*") {
        $res = self::where('id',$id)->field($field)->find();
        if(!$res) {
            return false;
        }
        return $res->toArray();
    }

    /* 多条 */
    public static function getAll ($where,$orderby='id desc',$field="*") {
        $res = self::where($where)->field($field)->order($orderby)->select();
        if($res->isEmpty()) {
            return false;
        }
        return $res->toArray();
    }

    /* 分页 */
    public static function getPage ($where,$page=1,$size=20,$orderby='id desc',$field="*") {
        $res = self::where($where)->page($page,$size)->order($orderby)->field($field)->select();
        if($res->isEmpty()) {
            return false;
        }
        return $res->toArray();
    }

    public static function getGroupby ($where,$page=1,$size=20,$orderby='id desc',$field="*",$groupby="create_time") {
        $res = self::where($where)->group($groupby)->page($page,$size)->order($orderby)->field($field)->select();
        if($res->isEmpty()) {
            return false;
        }
        return $res->toArray();
    }

    public static function getGroupByCount ($where,$groupby='create_time') {
        return $res = self::where($where)->group($groupby)->count();
    }

    public static function getCount ($where) {
        return $res = self::where($where)->count();
    }

    public static function setIncr($where,$field){
        return self::where($where)->setInc($field);
    }

    public static function getColumn($where,$field,$order='id asc'){
        return self::where($where)->order($order)->column($field);
    }

    public static function getValue($where,$field,$order='id asc'){
        return self::where($where)->order($order)->value($field);
    }


    public static function getMax ($where,$field) {
        return $res = self::where($where)->max($field);
    }

    public static function setDel ($where) {
        return $res = self::where($where)->delete();
    }

    public static function setSaveAll ($data) {
        return $res = self::insertAll($data);
    }

    public static function setInsertGetId($data) {
        return $res = self::insertGetId($data);
    }

    public static function setFieldInc ($where,$field,$number=1) {
        return $res = self::where($where)->setInc($field,$number);
    }

    public static function setFieldDec ($where,$field,$number=1) {
        return $res = self::where($where)->setDec($field,$number);
    }


    public static function getSum ($where,$field) {
        return $res = self::where($where)->sum($field);
    }

}